package com.horizon.mobile.activity;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.interfaces.OnBottomDragListener;
import zuo.biao.library.manager.HttpManager.OnHttpResponseListener;
import zuo.biao.library.util.JSON;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.horizon.mobile.R;
import com.horizon.mobile.manage.SharePreferences;
import com.horizon.mobile.model.Result;
import com.horizon.mobile.util.HttpRequest;

public class ConfigActivity extends BaseActivity implements
		OnBottomDragListener {

	EditText urlEt,urlWWEt,suffixEt;
	Button confirmBt,confirmWWBt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_config, this);

		intent = getIntent();

		// 功能归类分区方法，必须调用<<<<<<<<<<
		initView();
		initData();
		initEvent();
		// 功能归类分区方法，必须调用>>>>>>>>>>
	}

	@Override
	public Activity getActivity() {
		// TODO Auto-generated method stub
		return this;
	}

	@Override
	public void initView() {
		urlEt = (EditText) findViewById(R.id.urlEt);
		urlWWEt = (EditText) findViewById(R.id.urlWWEt);
		suffixEt = (EditText) findViewById(R.id.suffixEt);
		confirmBt = (Button) findViewById(R.id.confirmBt);
		confirmWWBt = (Button) findViewById(R.id.confirmWWBt);
	}

	@Override
	public void initData() {
		// TODO Auto-generated method stub
		urlEt.setText(SharePreferences.getInstance(context).getServerUrl(""));
		urlWWEt.setText(SharePreferences.getInstance(context).getServerUrlWW(""));
	}

	@Override
	public void initEvent() {
		confirmBt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if(urlEt.getText().toString().length()==0){
					showShortToast("内网地址不能为空！");
					return;
				}
				if(suffixEt.getText().toString().length()==0){
					showShortToast("后缀为空！");
					return;
				}
				showProgressDialog("正在连接，请稍后...");
				HttpRequest.connectTest("http://" + urlEt.getText().toString(),suffixEt.getText().toString(),
						0, connectListener);
			}
		});
		confirmWWBt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if(urlWWEt.getText().toString().length()==0){
					showShortToast("外网地址不能为空！");
					return;
				}
				if(suffixEt.getText().toString().length()==0){
					showShortToast("后缀为空！");
					return;
				}
				showProgressDialog("正在连接，请稍后...");
				HttpRequest.connectTest("http://" + urlWWEt.getText().toString(),suffixEt.getText().toString(),
						0, connectListener);
			}
		});
	}

	@Override
	public void onDragBottom(boolean rightToLeft) {
		// TODO Auto-generated method stub
		finish();
	}

	OnHttpResponseListener connectListener = new OnHttpResponseListener() {

		@Override
		public void onHttpResponse(int requestCode, String resultJson,
				Exception e) {
			// TODO Auto-generated method stub
			dismissProgressDialog();
			boolean isNull = resultJson == null;
			if (resultJson != null) {
				Result result = JSON.parseObject(resultJson, Result.class);
				if (result.isSuccess()) {
					showShortToast("配置成功！");
					SharePreferences.getInstance(context).putServerUrl(
							urlEt.getText().toString());
					SharePreferences.getInstance(context).putServerUrlWW(
							urlWWEt.getText().toString());
					SharePreferences.getInstance(context).putSuffix(suffixEt.getText().toString());
//					finish();
				}else{
					showShortToast("连接服务器失败！");
				}

			} else {
				if (e != null)
					showShortToast("链接失败，请检查地址是否正确！");
				else
					showShortToast("连接服务器失败！");
			}

		}
	};

}
